package ${basePackage}.service.impl;

import ${basePackage}.mapper.${modelNameUpperCamel}Mapper;
import ${basePackage}.pojo.${modelNameUpperCamel};
import ${basePackage}.service.${modelNameUpperCamel}Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.Page;
import ${basePackage}.utils.PageUtil;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;


/**
*
* @author ${author}
* @date ${date}
*/
@Service
public class ${modelNameUpperCamel}ServiceImpl implements ${modelNameUpperCamel}Service {
    @Resource
    private ${modelNameUpperCamel}Mapper ${modelNameLowerCamel}Mapper;



    @Override
    public List<${modelNameUpperCamel}> queryAll() {
        List<${modelNameUpperCamel}> ${modelNameLowerCamel} = ${modelNameLowerCamel}Mapper.selectAll();
            return ${modelNameLowerCamel};
    }

    @Override
    public PageUtil<${modelNameUpperCamel}> findListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<${modelNameUpperCamel}> pages = (Page<${modelNameUpperCamel}>) ${modelNameLowerCamel}Mapper.selectAll();
        PageUtil<${modelNameUpperCamel}> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
    }


    @Override
    public PageUtil<${modelNameUpperCamel}> pageQueryByAll(Integer pageNum, Integer pageSize, ${modelNameUpperCamel} ${modelNameLowerCamel}) {

        Example example=new Example(${modelNameUpperCamel}.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO 待实现 重新实现判断参数

        //if(!StringUtils.isEmpty(brand.getId())){
        //    criteria.andEqualTo("id",brand.getId());
        //}
        //if(!StringUtils.isEmpty(brand.getName())){
        //   criteria.andLike("name",brand.getName());
        //}
        PageHelper.startPage(pageNum,pageSize);
        Page<${modelNameUpperCamel}> pages = (Page<${modelNameUpperCamel}>) ${modelNameLowerCamel}Mapper.selectByExample(example);
        PageUtil<${modelNameUpperCamel}> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
     }


    @Override
    public boolean save${modelNameUpperCamel}(${modelNameUpperCamel} ${modelNameLowerCamel}) {
        int insert = ${modelNameLowerCamel}Mapper.insert(${modelNameLowerCamel});
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean update${modelNameUpperCamel}(${modelNameUpperCamel} ${modelNameLowerCamel}) {
        int i = ${modelNameLowerCamel}Mapper.updateByPrimaryKeySelective(${modelNameLowerCamel});
        if(i>0){
            return true;
        }
        return false;
    }


    @Override
    public boolean deleteById(Integer id) {
        int i = ${modelNameLowerCamel}Mapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public ${modelNameUpperCamel} findById(Integer id) {
        ${modelNameUpperCamel} ${modelNameLowerCamel} = ${modelNameLowerCamel}Mapper.selectByPrimaryKey(id);
        return ${modelNameLowerCamel};
    }
}
